package com.imot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imot.pojo.dto.AddExperimentDataDto;
import com.imot.pojo.entity.ExperimentFiles;
import com.imot.pojo.vo.ExperimentDataDetailsVo;
import com.imot.pojo.vo.ExperimentDataVo;
import com.imot.response.Result;
import com.imot.service.ExperimentDataService;
import com.imot.service.IExperimentFilesService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.io.File;

/**
 * ClassName: ExperimentDataImpl
 * Description:
 * date: 2023/11/30 10:00
 *
 * @author imot-jiah.wang
 */
@Service
@RequiredArgsConstructor
public class ExperimentDataImpl implements ExperimentDataService {
    private final IExperimentFilesService experimentFilesService;

    public static void main(String[] args) {
        try {
            Process process = Runtime.getRuntime().exec("systeminfo");
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));

            String line;
            while ((line = reader.readLine()) != null) {
                if (line.contains("物理内存总量")) {
                    System.out.println(line.trim());
                }
                if (line.contains("可用的物理内存")) {
                    System.out.println(line.trim());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public Result getStatusbar() {
        // 获取 C 盘的信息
        File cDrive = new File("C:");
        // 获取 D 盘的信息
        File dDrive = new File("D:");
        System.out.printf("C盘 总空间: %.2f GB\n", cDrive.getTotalSpace() / (double)(1024 * 1024 * 1024));
        System.out.printf("C盘 可用空间: %.2f GB\n", cDrive.getUsableSpace() / (double)(1024 * 1024 * 1024));

        System.out.printf("D盘 总空间: %.2f GB\n", dDrive.getTotalSpace() / (double)(1024 * 1024 * 1024));
        System.out.printf("D盘 可用空间: %.2f GB\n", dDrive.getUsableSpace() / (double)(1024 * 1024 * 1024));
        return null;
    }


    @Override
    public Result addExperimentData(AddExperimentDataDto addExperimentDataDto) {
        MultipartFile file = addExperimentDataDto.getFile();
        long sizeInBytes = file.getSize();
        // 将文件大小转换为MB
        double sizeInMB = sizeInBytes / 1024.0 / 1024.0;
        String s = sizeInMB + "MB";
        LocalDateTime now = LocalDateTime.now();
        ExperimentFiles experimentFiles = new ExperimentFiles();
        BeanUtils.copyProperties(addExperimentDataDto, experimentFiles);
        experimentFiles.setUploadTime(now);
        experimentFiles.setFileSize(s);
        experimentFilesService.save(experimentFiles);
        return Result.success();
    }

    @Override
    public Result deleteExperimentData(Integer id) {
        ExperimentFiles experimentFiles = new ExperimentFiles();
        experimentFiles.setId(id);
        experimentFiles.setIsDeleted(1);
        experimentFilesService.updateById(experimentFiles);

        return Result.success();
    }

    @Override
    public Result updateExperimentData(ExperimentFiles experimentFiles) {
        experimentFilesService.updateById(experimentFiles);
        return Result.success();
    }

    @Override
    public Result getExperimentData(Integer page, Integer limit, LocalDateTime start, LocalDateTime end, String exceptionType, String exceptionName) {
        if (page == null || limit == null) {
            page = 1;
            limit = 10;
        }
        PageHelper.startPage(page, limit);
        LambdaQueryWrapper<ExperimentFiles> wrapper = new LambdaQueryWrapper<>();
        if (start != null && end != null) {
            wrapper.ge(ExperimentFiles::getUploadTime, start);
            wrapper.le(ExperimentFiles::getUploadTime, end);
        }
        if (exceptionType != null) {
            wrapper.eq(ExperimentFiles::getDataType, exceptionType);
        }
        if (exceptionName != null) {
            wrapper.eq(ExperimentFiles::getExperimentName, exceptionName);
        }
        wrapper.eq(ExperimentFiles::getIsDeleted,0);


        List<ExperimentFiles> list = experimentFilesService.list(wrapper);
        PageInfo<ExperimentFiles> pageInfo = new PageInfo<>(list);
        HashMap<String, Object> map = new HashMap<>();
        List<ExperimentFiles> list1 = pageInfo.getList();

        List<ExperimentFiles> infoList = pageInfo.getList();
        ArrayList<ExperimentDataVo> experimentDataVos = new ArrayList<>();
        for (ExperimentFiles experimentFiles : infoList) {
            ExperimentDataVo experimentDataVo = new ExperimentDataVo();
            BeanUtils.copyProperties(experimentFiles,experimentDataVo);
            experimentDataVos.add(experimentDataVo);
        }

        map.put("total",pageInfo.getTotal());
        map.put("list",experimentDataVos);

        return Result.success(map);
    }

    @Override
    public Result getExperimentDataDetails(Integer id) {
        ExperimentDataDetailsVo experimentDataDetailsVo = new ExperimentDataDetailsVo();
        ExperimentFiles experimentFiles = experimentFilesService.getById(id);
        BeanUtils.copyProperties(experimentFiles,experimentDataDetailsVo);
        return Result.success(experimentDataDetailsVo);
    }
}

